package com.bnyy.medicalHousekeeper.moudle.message.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AlertDialog;

import com.bnyy.medicalHousekeeper.DialogHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.GroupChatSetting;
import com.bnyy.message.enums.GroupChatIdentity;

import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/11/20
 **/
public class GroupChatNoticeActivity extends BaseActivityImpl {
    @BindView(R.id.et_notice)
    EditText etNotice;
    @BindView(R.id.tv_count)
    TextView tvCount;

    public static void show(Context context, GroupChatSetting groupChatSetting) {
        show(context, groupChatSetting, null);
    }

    public static void show(Context context, GroupChatSetting groupChatSetting, ActivityResultLauncher<Intent> launcher) {
        Intent intent = new Intent(context, GroupChatNoticeActivity.class);
        intent.putExtra("groupChatSetting", groupChatSetting);
        if (launcher == null) {
            context.startActivity(intent);
        } else {
            launcher.launch(intent);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_message_group_chat_notice;
    }

    @Override
    public String getTitleStr() {
        return "群公告";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        GroupChatSetting groupChatSetting = (GroupChatSetting) intent.getSerializableExtra("groupChatSetting");
        String groupChatNotice = groupChatSetting.getGroup_board();
        boolean readOnly = groupChatSetting.getGroupChatIdentity() == GroupChatIdentity.MEMBER;

        if (readOnly) {
            etNotice.setFocusable(false);
            tvCount.setVisibility(View.GONE);
        } else {
            TextView tv = new TextView(mContext);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
            tv.setPadding(15, 5, 15, 5);
            tv.setBackgroundResource(R.drawable.selector_button_corner_green_180);
            tv.setText("发布");
            tv.setTextColor(Color.WHITE);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogHelper.showNormalDialog(mContext, "确认发布群公告吗？",
                            new DialogHelper.Callback() {
                                @Override
                                public void onRightClick(AlertDialog alertDialog) {
                                    super.onRightClick(alertDialog);
                                    String notice = etNotice.getText().toString();
                                    HashMap<String, Object> params = new HashMap<>();
                                    params.put("id", groupChatSetting.getId());
                                    params.put("group_board", notice);
                                    requestManager.request(requestManager.mMessageRetrofitService.editGroupChatSetting(RequestManager.getJsonRequestBody(params)),
                                            new BaseObserverImpl<Object>(mContext) {
                                                @Override
                                                public void onSuccess() {
                                                    super.onSuccess();
                                                    Intent intent = new Intent();
                                                    intent.putExtra("groupChatNotice", notice);
                                                    setResult(RESULT_OK, intent);
                                                    finish();
                                                }
                                            });
                                }
                            });
                }
            });
            setOptions(tv);

            etNotice.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (TextUtils.isEmpty(s)) {
                        tvCount.setText("0/200");
                    } else {
                        tvCount.setText(s.length() + "/200");
                    }
                }
            });
        }
        etNotice.setText(groupChatNotice);
    }
}








































































